package com.zhentao.service.impl;
import com.zhentao.dto.UserSearchDto;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.pojo.ApAssociateWords;
import com.zhentao.service.ApAssociateWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApAssociateWordsServiceImpl implements ApAssociateWordsService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult findAssociate(UserSearchDto userSearchDto) {
        if (userSearchDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //执行模糊查询
        Query query = new Query(Criteria.where("associateWords")
                .regex(".*" + userSearchDto.getSearchWords() + ".*")
        );


        List<ApAssociateWords> apAssociateWordsList =
                mongoTemplate.find(query, ApAssociateWords.class);

        return ResponseResult.okResult(apAssociateWordsList);
    }
}
